﻿using JetBrains.Annotations;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Unity.VisualScripting;
using UnityEngine;

public class GameManager : MonoBehaviour
{

    //单例模式
    private static GameManager instance;
    public static GameManager Instance
    {
        get
        {
            if (instance == null)
            {
                GameObject temp= new GameObject(nameof(GameManager));
                instance=temp.AddComponent<GameManager>();

            }
            return instance;
        }
    }

    public enum ItemType
    {
        Empty,
        Normal,
        Special
    }
    [Serializable]
    public struct Cell
    {
        public ItemType itemType;
        public GameObject cell;
    }
    public Cell[] itemPrefabs;
    public GameObject backGround;
    public int xNum;
    public int yNum;


    private Dictionary<ItemType, GameObject> itemPrefabDict;

    private GameBat startBat;
    private GameBat endBat;




    // Start is called before the first frame update
    void Start()
    {
        
        //实例化字典，并将不同状态添加进字典
        itemPrefabDict= new Dictionary<ItemType, GameObject>();
        for(int i=0;i<itemPrefabs.Length; i++)
        {
            if (!itemPrefabDict.ContainsKey(itemPrefabs[i].itemType))
            {
                itemPrefabDict.Add(itemPrefabs[i].itemType, itemPrefabs[i].cell);
            }
        }
        CreatGrid(xNum, yNum);
        CreatBat(xNum, yNum);
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    //生成网格
    public void CreatGrid(int m,int n)
    {
        for(int i=0;i<m;i++)
        {
            for(int j = 0; j < n; j++)
            {
                GameObject temp = Instantiate(itemPrefabDict[ItemType.Empty],new Vector3(5,5,0),Quaternion.identity,backGround.transform);
                temp.transform.localScale = new Vector3((float)0.1, (float)0.1, 0);
                temp.transform.localPosition=CorrectPosition(i,j);
            }
        }
    }
    //生成球拍
    public void CreatBat(int m,int n)
    {
        for(int i=0;i<m; i++)
        {
            for(int j = 0; j < n; j++)
            {
                GameObject temp = Instantiate(itemPrefabDict[ItemType.Normal],new Vector3(5,5,0), Quaternion.identity,backGround.transform);
                temp.transform.localScale=new Vector3(7.8f, 7.8f, 0);
                temp.transform.localPosition =CorrectPosition(i,j);
            }
        }
    }
    //纠正位置
    public Vector3 CorrectPosition(int m,int n) 
    { 
        Vector3 startPosition= new Vector3();
        startPosition.x = -2.57f;
        startPosition.y = 4.87f;
        float aDistance = 1.09f;
        float bDistance = 0.74f;
        Vector3 resualt = new Vector3(startPosition.x+m*bDistance,startPosition.y-n*aDistance,0);
        return resualt;
        
    }
    

    //鼠标的三种状态传递
    public void EnterBat(GameBat temp)
    {
        endBat = temp;
    }
    public void DownBat(GameBat temp)
    {
        startBat = temp;
        //可以播放选中动画
    }
    public void ReleaseBat()
    {
        //如果两个物体相邻，即可进行交换，否则不行

    }
}
